/**
 * 培训计划首次审核
 */
Ext.define('PJ.view.codtrainingprogram.FirstAudit', {
	extend: 'AM.default.DefaultWindowForm',
	requires: ['AM.default.jsonp.DefaultSubmitJsonp',
	           'AM.default.DefaultDateField',
	           'AM.default.DateTime',
	           'PJ.view.trainingprogram.ParticipantsNumList',
	           'PJ.view.trainingmanagement.CourseTree'
	           ],

	modal: true,// 开启不允许点击其他组件
	frame: true,
	notEdit: true,
	isSame: false,
    width: 515,
    height:585,
	layout: 'form',
	overflowX: 'hidden',
    overflowY: 'scroll',
    rejectReason: null,
	initComponent: function(){
		var me = this;
//    	var bt = '<span style="color:red;">*</span>';
        var theDate = new Date();
		me.items = [{
			xtype: 'container',
			border: false,
			layout: 'column',
    		width:980,
    		defaults:{
				labelAlign: 'right',
				allowBlank: true,  //红框是否显示
				padding: '0 0 10 0',
				columnWidth: .49,
				readOnly:true
			},
			items:[{
				xtype: 'container',
				border: false,
				layout: 'column',
				items:[{
					xtype: 'container',
					border: false,
					layout: 'column',
					columnWidth: .99,
					defaults:{
						labelAlign: 'right',
						allowBlank: true,  //红框是否显示
						padding: '5 0 0 0',
						labelWidth: 90,
						readOnly:false,
						columnWidth: .99,
						labelSeparator: ''
					},
					items:[{
	        			xtype:"textfield",
	            		fieldLabel:"培训班名称:",
	            		name:"classNameNew",
	            		padding: '0 0 0 0'
	        		},{
	        			xtype:"textfield",
	            		fieldLabel:"计划申请编号:",
	            		name:"applyNumNew",
	            		blankText:"计划申请编号不能为空!",
	            		readOnly: true
	        		},{
	        			xtype:"textfield",
	            		fieldLabel:"承办部委:",
	            		name:"undertakeNameNew",
	            		blankText:"承办部委不能为空!",
	            		readOnly: true
	        		},{
	        			xtype:"textfield",
	            		fieldLabel:"合办部委:",
	            		name:"jointNameNew",
	            		blankText:"合办部委不能为空!",
	            		columnWidth:.86,
	            		readOnly: true
	        		},{
	        			xtype: "button",
	        			columnWidth:.06,
	        			tooltip: '合办部委分类',
	        			margin: "5 0 0 3",
	        			padding: "2 0 2 0",
	        			editable: false,
	        			iconCls:"configIco",
	        			hidden:false,
	        			readOnly: true,
	        			handler: function(button){
	        				Ext.create('AM.default.DefaultWindowView', {
	        					title: "选取合办部委",
	        					width: 300,
	        					height:450,
	        					grid:me,
	        					items:[{
	        						xtype:"trainingTree",
	        	                    listeners: {
	        	                        // 叶子节点点击事件,如果非叶子节点与此动作相同，需覆盖itemclick事件
	        	                        itemClick: function(view, record, htmlelement, index, eventObject, object){
	        	                        	if (record.data.id != -1) {
	        	                        		eventObject.stopEvent();
	        	                        		var values = record.data.text;
	        	                        		var ids = record.data.id;
	        	                        		var  baseForm = me.down('form').getForm();
	        	                        		var jointName = baseForm.findField('jointNameNew');
	        	                        		var jointIdNew = baseForm.findField('jointIdNew');
	        	                        		jointName.setValue(values);
	        	                        		jointIdNew.setValue(ids);
	        	                        		this.up('window').close();
	        	                        	}
	        	                        },
	        	                        //显示树的一级菜单
	        	                        load: function(treeStore, node, records, successful, eOpts){
	        	                            if (records != null && node.hasChildNodes()) {
	        	                                this.expandNode(records[0]);
	        	                                isExp = false;
	        	                            }
	        	                        }
	        	                    }
	        					}]
	        				});
	        			}
	        		},{
	        			xtype: "button",
	        			columnWidth:.06,
	        			tooltip: '清除',
	        			margin: "5 0 0 3",
	        			padding: "2 0 2 0",
	        			editable: false,
	        			iconCls:"deleteIco",
	        			hidden:false,
	        			handler: function(button){
	        				var form = button.up("form");
	        				var baseForm = form.getForm();
	        				baseForm.findField("jointNameNew").setValue("");
	        			}
	        		}/*,{
	        			xtype:"textfield",
	            		fieldLabel:"计划时间:",
	            		name:"planDateNew",
	            		maxLength: 50
	        		}*/, {
	    				xtype: 'defaultDate',
	    				fieldLabel: '计划时间',
	    				name: 'startPlanDateNewStr',
	    				id: 'startPlanDateNewStr',
	    				vtype: 'startLessEndDate',
	        			width: 50,
	    				startLessEndDate: {
	    					targetCmpId: 'endPlanDateNewStr',
	    					isStart: true
	    				},
	    				margin: '5 0 0 0',
	    				emptyText: "请输入计划开始时间",
//	    				disabled:me.isDisable,
	    				allowBlank: false,
	        			readOnly:false,
	        			value: theDate,
	                    listeners: {
	                    	change: function(textField){
	                    		var baseForm = me.down("form").getForm();
	                    		var endPlanDateNewStr = baseForm.findField("endPlanDateNewStr").getValue();
	                    		var startPlanDateNewStr = textField.getValue();
	                    		var endTime = endPlanDateNewStr.getTime();
	                    		var startTime = startPlanDateNewStr.getTime();
	                    		var num = (endTime - startTime)/1000/60/60/24;
	                    		baseForm.findField("planDayNew").setValue(num+1);
	                    	}
	                    }
	    			}, {
	    				xtype: 'defaultDate',
	    				fieldLabel: '至',
	    				name: 'endPlanDateNewStr',
	    				id: 'endPlanDateNewStr',
	    				vtype: 'startLessEndDate',
	        			width: 50,
	    				vtypeText: '结束时间不能早于起始时间！',
	    				startLessEndDate: {
	    					targetCmpId: 'startPlanDateNewStr',
	    					isStart: false
	    				},
	    				margin: '5 0 0 0',
	    				emptyText:  me.flag==4?"":"请输入计划结束时间",
//	    				disabled:me.isDisable,
	        			allowBlank: false,
	        			readOnly:false,
	        			value: theDate,
	                    listeners: {
	                    	change: function(textField){
	                    		var baseForm = me.down("form").getForm();
	                    		var endPlanDateNewStr = textField.getValue();
	                    		var startPlanDateNewStr = baseForm.findField("startPlanDateNewStr").getValue();
	                    		var endTime = endPlanDateNewStr.getTime();
	                    		var startTime = startPlanDateNewStr.getTime();
	                    		var num = (endTime - startTime)/1000/60/60/24;
	                    		baseForm.findField("planDayNew").setValue(num+1);
	                    	}
	                    }
	    			},{
	        			xtype: 'numberfield',
	        			fieldLabel: '学制(天)',
	        			name: 'planDayNew',
	        			disabled:me.flag==3?true:false,
	        			allowBlank: me.isReadOnly,
	        			readOnly:true,
	    				hideTrigger: true,
	    				vtype: 'blankTrim',
	    				minValue:0.1,
	    				maxValue: 99999,
	    				decimalPrecision: 1,
	    				value:1
	        		},{
	            		xtype:"textareafield",
	            		fieldLabel:"办班目的:",
	        			allowBlank: me.isReadOnly,
	            		name:"classObjectiveNew"
	            	},{
	            		xtype:"textareafield",
	            		fieldLabel:"培训内容:",
	        			allowBlank: me.isReadOnly,
	            		name:"classValuesNew"
	            	},{
	            		xtype:"textareafield",
	            		fieldLabel:"调训对象:",
	        			allowBlank: me.isReadOnly,
	            		name:"trainingObjectNew"
	            	},{
	            		xtype:"textfield",
	            		fieldLabel:"培训地点:",
	        			allowBlank: me.isReadOnly,
	            		name:"trainingAddressNew"
	            	},{
	            		xtype:"textfield",
	            		fieldLabel:"联系人:",
	        			allowBlank: me.isReadOnly,
	            		name:"linkManNew"
	            	},{
	            		xtype:"textfield",
	            		fieldLabel:"联系电话:",
	        			allowBlank: me.isReadOnly,
	            		name:"linkModeNew"
	            	},{
	            		xtype:"textfield",
	            		fieldLabel:"传真:",
	        			allowBlank: me.isReadOnly,
	            		name:"faxNew"
	            	},{
	            		xtype: 'participantsNumList',
	            		versionState: false
	            	},{
	            		xtype: 'hidden',
	            		name: 'planIdNew'
	            	},{
	            		xtype: 'hidden',//合办部委id
	            		name: 'jointIdNew'
	            	},{
	            		xtype: 'hidden',//承办部委id
	            		name: 'undertakeIdNew'
	            	}]
				}]
			}]
		}];
		this.buttons = [{
			text:"保存",
			scope:this,
			handler:function(button, event){
                var win = button.up('window');
                win.sub("wqq");
            }
		},{
			text:"初审通过",
			scope:this,
			handler:function(button, event){
                var win = button.up('window');
                Ext.Msg.confirm('提示','确认初审通过吗？',function(button){
                	if(button=='yes'){
                		win.sub("next");
                	}
                });
            }
		},{
			text:"建议修改",
			scope:this,
			handler:function(button, event){
                var win = button.up('window');
                Ext.create('PJ.view.codtrainingprogram.AuditOpinionEdit',{
                	title: "建议修改内容",
                    flag:1,
//                    classIds: classIds,
                	grid: me.grid,
                	isView: false,
                	win: win
                });
            }
		},{
			text:"否决",
			scope:this,
			handler:function(button, event){
                var win = button.up('window');
                Ext.Msg.confirm('提示','确认否决吗？',function(button){
                	if(button=='yes'){
                		win.sub("reject");
                	}
                });
            }
		},{
			text: '关闭',
			scope: this,
			handler: this.close
		}];
		this.callParent(arguments);
	},
	sub: function(flag){
    	var win = this;
    	var form = win.down('form');
    	var baseForm = form.getForm();
    	if (!baseForm.isValid()) {
    		return;
    	}
    	var searchAuditState = 3;
		var editUrl = win.grid.store.examinationPassedUrl;//保存
		if(flag == "next"){
			editUrl = win.grid.store.examinationPassedUrl;//审核通过
			searchAuditState = 1;
		}else if(flag == "back"){
			editUrl = win.grid.store.examinationPassedUrl;//审核驳回
			searchAuditState = 2;
		}else if(flag == "reject"){
			editUrl = win.grid.store.examinationPassedUrl;//审核否决
			searchAuditState = 5;
		}
		
		var className = baseForm.findField("classNameNew").getValue();
		var classObjective = baseForm.findField("classObjectiveNew").getValue();
		var classValues = baseForm.findField("classValuesNew").getValue();
		var planDay = baseForm.findField("planDayNew").getValue();
		var jointName = baseForm.findField("jointNameNew").getValue();
		var undertakeName = baseForm.findField("undertakeNameNew").getValue();
//		var planDate = baseForm.findField("planDateNew").getValue();
		var startPlanDateStr = baseForm.findField("startPlanDateNewStr").getValue();
		var endPlanDateStr = baseForm.findField("endPlanDateNewStr").getValue();
		var trainingObject = baseForm.findField("trainingObjectNew").getValue();
		var trainingAddress = baseForm.findField("trainingAddressNew").getValue();
		var linkMan = baseForm.findField("linkManNew").getValue();
		var linkMode = baseForm.findField("linkModeNew").getValue();
		var fax = baseForm.findField("faxNew").getValue();
		var planIdNew = baseForm.findField("planIdNew").getValue();
		var jointIdNew = baseForm.findField("jointIdNew").getValue();
		var undertakeIdNew = baseForm.findField("undertakeIdNew").getValue();
		
		var participantsNumList = win.down("participantsNumList");
		participantsNumList.getSelectionModel().selectAll();
		var records = participantsNumList.getSelectionModel().getSelection();
		var deptIds = new Array();
		var deptNames = new Array();
		var countyPlanCounts = new Array();
		var cityPlanCounts = new Array();
		var planCounts = new Array();
		for(i in records){
        	if(isNaN(i)){
        		continue;
        	}
			deptIds.push(records[i].get("objectID"));
			deptNames.push(records[i].get("contrastObject"));
			countyPlanCounts.push(records[i].get("contrastCountyValue"));
			cityPlanCounts.push(records[i].get("contrastCityValue"));
			planCounts.push(records[i].get("contrastCountValue"));
		}
		if(gj_onlineLearning_constant.loadType == "jsonp"){
			win.down('panel').getForm().doAction("submitJsonp",{
				url: editUrl,
				params: {
					searchPlanId: planIdNew,
					deptIds: deptIds,
					deptNames: deptNames,
					countyPlanCounts: countyPlanCounts,
					cityPlanCounts: cityPlanCounts,
					planCounts: planCounts,
					searchAuditState: searchAuditState,
					rejectReason: win.rejectReason,
					className: className,
					classObjective: classObjective,
					classValues: classValues,
					planDay: planDay,
					jointName: jointName,
					jointId: jointIdNew,
					undertakeName: undertakeName,
					undertakeId: undertakeIdNew,
//					planDate: planDate,
					trainingObject: trainingObject,
					trainingAddress: trainingAddress,
					linkMan: linkMan,
					linkMode: linkMode,
					fax: fax,
					startPlanDateNewStr:startPlanDateStr,
					endPlanDateNewStr:endPlanDateStr
				},
				success: function(f, action){
					if (action.result.success) {
						Ext.Msg.alert('提示', '操作成功！', function(){
							win.grid.getStore().load();
							win.close();
						});
					}else{
						Ext.Msg.alert('提示', action.result.msg, function(){
							win.close();
						});
					}
				},
				failure: function(f, action){
					if (action.result) {
						Ext.Msg.alert('提示', action.result.msg, function(){
							win.close();
						});
					}
					else {
						Ext.Msg.alert('提示', '操作失败！', function(){
							win.close();
						});
					}
				}
			});
		}else{
        	win.submit({
    			url: editUrl,
    			params: {
    				searchPlanId: planIdNew,
    				deptIds: deptIds,
					deptNames: deptNames,
					countyPlanCounts: countyPlanCounts,
					cityPlanCounts: cityPlanCounts,
					planCounts: planCounts,
					searchAuditState: searchAuditState,
					rejectReason: win.rejectReason,
					className: className,
					classObjective: classObjective,
					classValues: classValues,
					planDay: planDay,
					jointName: jointName,
					jointId: jointIdNew,
					undertakeName: undertakeName,
					undertakeId: undertakeIdNew,
//					planDate: planDate,
					trainingObject: trainingObject,
					trainingAddress: trainingAddress,
					linkMan: linkMan,
					linkMode: linkMode,
					fax: fax,
					startPlanDateNewStr:startPlanDateStr,
					endPlanDateNewStr:endPlanDateStr
				},
        		success: function(f, action){
        			if (action.result.success) {
        				Ext.Msg.alert('提示', '操作成功！', function(){
        					win.grid.getStore().load();
            				win.close();
        				});
        			}else{
        				Ext.Msg.alert('提示', action.result.msg, function(){
        					win.close();
        				});
        			}
        		},
        		failure: function(f, action){
        			if (action.result) {
        				Ext.Msg.alert('提示', action.result.msg, function(){
        					win.close();
        				});
        			}
        			else {
        				Ext.Msg.alert('提示', '操作失败！', function(){
        					win.close();
        				});
        			}
        		}
        	});
        }
    }
});